<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            height: 2200px;
        }

        .nav{
            height: 50px;
            background:red;
            position: fixed;
            left:0;
            top:0;
            width: 100%; 
            background: transparent;
        }
    </style>
</head>

<body>
    <div class="nav" id="nav">这是一个NAV</div>
    <script>
        // onscroll 滚动事件
        var nav=document.getElementById("nav");

        window.onscroll = function () {
            // 获取滚动条距离顶部的高度 获取的是拉动的部分的高度；
            var scrollTop = document.documentElement.scrollTop;
            console.log(scrollTop);
            // 当滚动条距离顶部的高度达到300的时候完全显示
            var opacity=scrollTop/300;
            if(opacity>1) opacity=1;
            nav.style.opacity=opacity;
            nav.style.backgroundColor="rgb(33,125,133)"
        }
        
    </script>
</body>

</html>